package org.walklink.common.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程工厂
 *
 * @author lidishan
 */
public class NamedThreadFactory implements ThreadFactory {
    private final String prefix;
    private final AtomicInteger mThreadNum;
    private final boolean daemon;

    public NamedThreadFactory(String mark) {
        this(mark, false);
    }

    public NamedThreadFactory(String prefix, boolean daemon) {
        this.mThreadNum = new AtomicInteger(1);
        this.prefix = prefix;
        this.daemon = daemon;
        SecurityManager s = System.getSecurityManager();
    }

    @Override
    public Thread newThread(Runnable runnable) {
        String name = this.prefix + "_" + this.mThreadNum.getAndIncrement();
        Thread ret = new Thread(runnable, name);
        ret.setDaemon(this.daemon);
        return ret;
    }
}
